// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-23

package list

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/kind"
	"server/app/lib"
)

// Logic
// 加载会员列表.
type Logic struct {
	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	ids    []kind.UserId
	mapper map[kind.UserId]*Response
	total  int64

	request  *Request
	response []*Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: make([]*Response, 0),
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Query(i, o.request); err == nil {
		o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
		o.ids = make([]kind.UserId, 0)
		o.mapper = make(map[kind.UserId]*Response)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
	)
	if err = db.Transaction(ctx,
		o.RunMember,
		o.RunUser,
	); err != nil {
		return res.WithError(errs.ErrDatabaseQuery)
	}

	for _, k := range o.ids {
		if v, ok := o.mapper[k]; ok {
			o.response = append(o.response, v)
		}
	}
	return res.WithPaging(o.response, o.total, o.request.Page, o.request.Size)
}

func (o *Logic) RunMember(_ context.Context, sess *db.Session) (err error) {
	var list []*models.ArcadeMember

	// 1. 统计总数.
	if o.total, err = sess.Where("arcade_id = ?", o.arcade.Id).Count(models.ArcadeMember{}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if o.total == 0 {
		return
	}

	// 2. 读取列表.
	if err = sess.Where("arcade_id = ?", o.arcade.Id).
		Limit(o.request.Size, o.request.Offset).
		Find(&list); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else {
		for _, v := range list {
			o.ids = append(o.ids, v.UserId)
			o.mapper[v.UserId] = (&Response{}).With(v)
		}
	}
	return
}

func (o *Logic) RunUser(_ context.Context, sess *db.Session) (err error) {
	var list []*models.Users

	if len(o.ids) == 0 {
		return
	}

	if err = sess.In("id", o.ids).Find(&list); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	for _, v := range list {
		if response, ok := o.mapper[v.Id]; ok {
			response.WithUser(v)
		}
	}

	return
}

// func (o *Logic) Run2(ctx context.Context, _ iris.Context) result.Result {
//     var (
//         err error
//         res = result.New()
//     )
//
//     if err = o.RunMember(ctx); err != nil {
//         return res.WithError(err)
//     }
//     return res.WithPaging(o.response, o.total, o.request.Page, o.request.Size)
// }

// func (o *Logic) RunMember(ctx context.Context) (err error) {
//     var (
//         ids    = make([]kind.UserId, 0)
//         keys   = make([]kind.UserId, 0)
//         mapper = make(map[kind.UserId]*Response)
//         ml     []*models.ArcadeMember
//         ms     = services.NewArcadeMemberService()
//         ul     []*models.Users
//         us     = services.NewUsersService()
//     )
//
//     // 1. 读取列表.
//     if ml, o.total, err = ms.Paging(ctx, map[string]any{}, o.request.Page, o.request.Size); err != nil {
//         err = errs.ErrDatabaseQuery
//         return
//     }
//
//     // 2. 读取详情.
//     for _, item := range ml {
//         mapper[item.UserId] = (&Response{}).With(item)
//
//         ids = append(ids, item.UserId)
//     }
//
//     // 3. 读取资料.
//     if ul, err = us.Dao.ListByMap(ctx, map[string]any{
//         "id": ids,
//     }); err != nil {
//         err = errs.ErrDatabaseQuery
//         return
//     } else {
//         for _, item := range ul {
//             if response, ok := mapper[item.Id]; ok {
//                 response.WithUser(item)
//             }
//         }
//     }
//
//     for _, k := range keys {
//         if v, ok := mapper[k]; ok {
//             o.response = append(o.response, v)
//         }
//     }
//     return
// }
